<template>
    <el-container>
        <el-aside width="180px">
            <el-menu >
                <el-menu-item @click="onSeletType(item.id)" :index="item.id" :key="item.id" v-for="item in typeData">{{item.title}}</el-menu-item>
            </el-menu>
        </el-aside>
        <el-main class="pt0">
            <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
                <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
                <el-table-column label="名称" show-overflow-tooltip align="center">
                    <template #default="scope">
                        <el-text v-if="form.model == 'ProjectInvoice'">{{scope.row.project.title}}</el-text>
                        <el-text v-if="form.model == 'MaterialPay'">{{scope.row.material_pay.title}}</el-text>
                        <el-text v-if="form.model == 'LaborPay'">{{scope.row.labor_pay.b_name}}</el-text>
                        <el-text v-if="form.model == 'Reimburse'">{{scope.row.reimburse.title}}</el-text>
                    </template>
                </el-table-column>
                <el-table-column label="所属项目" show-overflow-tooltip align="center">
                    <template #default="scope">
                        <el-text>{{scope.row.project.title}}</el-text>
                    </template>
                </el-table-column>
                <el-table-column label="申请人" show-overflow-tooltip align="center">
                    <template #default="scope">
                        <el-text>{{scope.row.user.name}}</el-text>
                    </template>
                </el-table-column>
                <el-table-column prop="price" label="金额" align="center" />
                <el-table-column prop="created_at" label="申请时间" width="180" align="center" />
                <el-table-column label="状态" show-overflow-tooltip align="center">
                    <template #default="scope">
                        <el-text v-if="scope.row.status == 'pending'">审批中</el-text>
                        <el-text v-if="scope.row.status == 'approved'">已通过</el-text>
                        <el-text v-if="scope.row.status == 'rejected'">已驳回</el-text>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="80" align="center">
                    <template #default="scope">
                        <el-link v-if="scope.row.status == 'pending'"  type="primary" @click="onEdit(scope.row.id)" >审批</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </el-main>
    </el-container>
    <div class="mt20">
        <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  
    <el-dialog v-model="isEdit" title="申请操作" width="60%">
        <component :is="applyForm" :data = editId @onSave="onSave" />
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import applyForm from '@/components/applyPayForm.vue';
import http from '../../../util/request';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',model:'Reimburse',page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
const isEdit = ref<boolean>(false)
const editId = ref<number>(0)
const tableData = ref<any[]>([])
const typeData = ref<any>([
    // {title:'甲方开票',id:'ProjectInvoice'},
    // {title:'材料付款',id:'MaterialPay'},
    // {title:'施工班组付款',id:'LaborPay'},
    {title:'报销',id:'Reimburse'},
])

const onGetData = () => {
    
    form.value.loading = true
    tableData.value = []
    http.post('/approve',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSeletType = (data:string) => {
    form.value.model = data
    onGetData()
}

const onEdit = (id: number) => {
    editId.value = id
    isEdit.value = true
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
}
onMounted(()=>{
    onGetData()
})

const onSave = () => {
    isEdit.value = false
    onGetData()
}

</script>

<style scoped>

</style>